//index.js
const app = getApp()
const Service = app.require('/utils/Service.js');
const Util = app.require('/utils/Util.js');

Page({
  data: {
    staff: {},
    temperatureFocused: false,
  },
  onLoad: function() {
    let staff = wx.getStorageSync("staff")
    if (!staff) {
      staff = {}
    }
    staff.createTime = Util.formatTime(new Date())
    staff.temperature = null
    this.setData({
      staff: staff,
      temperatureFocused: staff.company != null && staff.name != null && staff.phone != null
    })
  },
  submitForm: function(form) {
    let data = form.detail.value
    console.log(data)
    if (!data.company) {
      return this.alert('请填写公司名称')
    }
    if (!data.name) {
      return this.alert('请填写姓名')
    }
    if (!data.phone) {
      return this.alert('请填写手机号码')
    }
    if (data.phone.length < 11) {
      return this.alert('请填写11位手机号码')
    }
    if (!data.temperature) {
      return this.alert('请填写体温')
    }
    wx.setStorageSync('staff', data)
    Service.saveStaff(data).then(() => {
      wx.navigateTo({
        url: 'success',
      })
    })
  },
  alert: function(msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  }
})